 //
//  MCLRTablView.m
//  MCmeituan
//
//  Created by Mc on 15/12/13.
//  Copyright © 2015年 Mc. All rights reserved.
//

#import "MCLRTablView.h"
#import "MCfenleiData.h"
#import "MCRTableViewCell.h"
#import "MCTableViewCell.h"


@interface MCLRTablView () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *leftCell;
@property (weak, nonatomic) IBOutlet UITableView *rightCell;
@property (nonatomic, assign) NSInteger leftRow;

@property (nonatomic, strong) NSArray *subfenlei;

@end

@implementation MCLRTablView

+ (instancetype)lrTableview
{

    return [[[NSBundle mainBundle] loadNibNamed:@"MCLRTableView" owner:nil options:nil] lastObject];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.leftCell) {
        return [self.datasource numberOfRowsInSection:self];
    }else
        return self.subfenlei.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = nil;
    if (tableView == self.leftCell) {

        
        cell = [MCTableViewCell leftCellWithTableView:tableView];
       
        
        cell.textLabel.text = [self.datasource lrTableView:self titleInLeftRow:indexPath.row];
        
        if ([self.datasource respondsToSelector:@selector(lrTableView: iconInLeftRow:)]) {
            NSString *iconStr = [self.datasource lrTableView:self iconInLeftRow:indexPath.row];
            cell.imageView.image = [UIImage imageNamed:iconStr];
        }
       
        if ([self.datasource respondsToSelector:@selector(lrTableView: hightlightIconInLeftRow:)]) {
            NSString *hightStr = [self.datasource lrTableView:self hightlightIconInLeftRow:indexPath.row];
            cell.imageView.highlightedImage = [UIImage imageNamed:hightStr];
        }
      
    }else{
        
        cell = [MCRTableViewCell rightCellWithTableView:tableView];
        
        cell.textLabel.text = self.subfenlei[indexPath.row];
        
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.leftCell) {
        
        
        self.subfenlei = [self.datasource lrTableView:self subDataInlLeftRow:indexPath.row];
        
        [self.rightCell reloadData];
        
        self.leftRow = indexPath.row;
    }else{
        if ([self.delegate respondsToSelector:@selector(lrTableView:leftDidSelect:andRightRow:)]){
            
            [self.delegate lrTableView:self leftDidSelect:self.leftRow
                    andRightRow:indexPath.row];
            
        }
        
    }
}
@end
